package com.aote.model.block;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.ExtendElement;
import com.browsesoft.Tools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;

/**
 * 各层无关的表头模型，每层有单独的数据模型
 */
public class MutexTableHeadModel implements MultiLevelTableHeadModel {
	/**
	 * 各层对应的模型，key=层，value=模型
	 */
	private Map models = new HashMap();
	
	/**
	 * 总共多少层
	 */
	private int level;

	public MutexTableHeadModel() {
		super();
	}

	public int getColumnCount(int level) {
		HTMLTableModel model = (HTMLTableModel) models.get(new Integer(level));
		return model.getColumnCount();
	}

	public String getColumnName(int level, int i) {
		HTMLTableModel model = (HTMLTableModel) models.get(new Integer(level));
		return model.getColumnName(i);
	}

	public void init(HttpServletRequest request) throws Exception {
		Iterator iter = this.models.values().iterator();
		while(iter.hasNext()) {
			HTMLTableModel model = (HTMLTableModel)iter.next();
			model.init(request);
		}
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Iterator iter = this.models.values().iterator();
		while(iter.hasNext()) {
			HTMLTableModel model = (HTMLTableModel)iter.next();
			model.service(request, response);
		}
	}

	public void setComponent(HTMLBasicComponent component) {
		try {
			// 对于每一层，得到模型，并设置模型
			Element e = component.getElement();
			level = ExtendElement.getIntegerAttr(e, "level", 1);
			for (int i = 1; i <= level; i++) {
				String attr = "model" + i;
				String cname = e.getAttribute(attr);
				HTMLTableModel model;
				model = (HTMLTableModel) Class.forName(cname).newInstance();
				// 放置模型
				this.models.put(new Integer(i), model);
				// 给模型设置元素
				model.setComponent(component);
			}
		} catch (InstantiationException e1) {
			throw new RuntimeException(e1);
		} catch (IllegalAccessException e1) {
			throw new RuntimeException(e1);
		} catch (ClassNotFoundException e1) {
			throw new RuntimeException(e1);
		}
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
		// 每一层模型更新
		Iterator iter = this.models.values().iterator();
		while(iter.hasNext()) {
			HTMLTableModel model = (HTMLTableModel)iter.next();
			model.updatePageElement(c);
		}
	}

	public int getColumnCount() {
		int result = 1;
		// 各层乘积
		Iterator iter = this.models.values().iterator();
		while(iter.hasNext()) {
			HTMLTableModel model = (HTMLTableModel)iter.next();
			int count = model.getColumnCount();
			result *= count;
		}
		return result;
	}

	public String getColumnName(int index) {
		LinkedList result = new LinkedList();
		int sum = index;
		// 循环除，得到每层的列名，组合成以逗号分割的字符串
		for(int i = level; i >= 1; i--) {
			HTMLTableModel model = (HTMLTableModel)this.models.get(new Integer(i));
			int count = model.getColumnCount();
			String name = model.getColumnName(sum % count);
			result.add(0, name);
			sum = sum / count;
		}
		String str = Tools.LinkedListTostring(result); 
		return str;
	}

	public int getRowCount() {
		return 0;
	}

	public String getType(int col) {
		return null;
	}

	public void setCondition(String condition) {
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
	}
}
